'''
@date 18-Feb-12
@author: Tyson Hamilton
@summary: The test library for Robot Framework.
'''
import os
from subprocess import Popen, PIPE, STDOUT

class TestFindLibrary:
    """ Library class for Robot Framework.
    
    Very simple library to facilitate TDD.
    """
    
    def __init__(self):
        """ Constructor
        
        Sets the SUT path and an initial status.
        """
        self._find_path = os.path.join(os.path.dirname(__file__),
                                      '..', 'src', 'find.py')
        self._status = ''
        
    def call_script(self, command, expression, path): 
        self._run_command(command, expression, path)

    def check_status(self, expected_status):
        if expected_status != self._status:
            raise AssertionError("Expected status to be '%s' but was '%s'"
                                  % (expected_status, self._status))
                       
    def _run_command(self, command, expression, path):
        """ Calls the SUT script with passed parameters.
        
        The output produced by the SUT is trimmed of all 
        newline and carriage returns to make testing the
        output easier with Robot Framework.
        """
        p = Popen([self._find_path, command, expression, path], shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
        self._status = p.stdout.read().replace("\n","").replace("\r","")
